/**
 * 好大夫
 * 好大夫疾病详情采集
 * @type {exports}
 */
module.exports = class {
  constructor(page) {
    this.page = page;
  }

  async pre() {
    const { page } = this;
    const href = await page.evaluate(() => {
      return document.querySelector(".dis-obj a").href;
    });
    await page.goto(href);
  }

  async evaluate() {
    const { page } = this;
    const data = await page.evaluate(() => {
      const nameEl = document.querySelector(".dis_description h1");
      const jieshaoEl = document.querySelector(".dis_description p");
      const items = document.querySelectorAll(".recommend_main");
      const nodeList = [];
      // eslint-disable-next-line no-unused-vars
      for (const item of items) {
        nodeList.push({
          name: item.querySelector("h2").textContent,
          description: item.querySelector("p.js-longcontent").textContent
        });
      }
      return {
        name: nameEl.textContent,
        description: jieshaoEl.textContent,
        nodeList
      };
    });
    return data;
  }

  async post(data) {
    const cate = await think
      .model("cate")
      .where({ name: data.cate_name })
      .find();
    if (cate && cate.id) data.cate_id = cate.id;
    const id = await think.model("wiki").thenAdd(data, { name: data.name });
    await think.model("wiki_meta").save({ wiki_id: id.id, ...data });
  }
};
